﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core;

namespace ThisIsFirstTest.Filter
{
    public class AccountAuthorizeAttribute : AuthorizeAttribute
    {
        public Role AccountRoles { get; set; }

        //public override void OnAuthorization(AuthorizationContext filterContext)
        //{
        //    base.OnAuthorization(filterContext);
        //}
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null || !httpContext.User.Identity.IsAuthenticated)
            {
                return false;
            }
            var logonAccount = CoreService.AccountService.AccountDetail(new Account { Id = httpContext.User.Identity.Name });
            if (logonAccount == null)
            {
                return false;
            }
            if (((Role)logonAccount.Roles.Roles & this.AccountRoles) == 0)
            {
                return false;
            }
            return base.AuthorizeCore(httpContext);
        }
    }
}